<template>
  <view>
    <!-- 搜索区域 -->
    <my-search @gotoSearch="goSearch"></my-search>
    <!-- 分类区域 -->
    <view class="scroll-view-container">
      <!-- 左侧滚动条区域 -->
      <scroll-view class="left-scroll-view" :style="{height: windowHeight + 'px'}" scroll-y>
        <block v-for="(item,i) in cateList" :key="i">
          <view :class="['left-scroll-view-item',i == active ? 'active' : '']" @click="activeChanged(i)">
            {{item.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧显示区域 -->
      <scroll-view class="right-scroll-view" :style="{height: windowHeight + 'px'}" scroll-y :scroll-top="scrollTop">
        <block v-for="(item,i) in secondList" :key="i">
          <view class="right-scroll-top-title">/ {{item.cat_name}} /</view>
          <view class="right-scroll-bottom-box">
            <view class="right-scroll-item" v-for="item2 in item.children" :key="item2.cat_id"
              @click="gotoGoodsList(item2)">
              <image :src="item2.cat_icon | setUrl" mode=""></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </block>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  // 导入自己封装的 mixin 模块
  import badgeMix from '@/mixins/tabbar-badge.js'


  export default {
    // 将 badgeMix 混入到当前的页面中进行使用
    mixins: [badgeMix],
    data() {
      return {
        // 当前屏幕可用高度
        windowHeight: 0,
        // 分类数据列表
        cateList: [],
        // 二级分类
        secondList: [],
        // 控制滚动条是否被选中
        active: 0,
        scrollTop: 0
      };
    },
    methods: {
      // 点击跳转到搜索界面
      goSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      },
      // 点击跳转商品列表
      gotoGoodsList(row) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + row.cat_id
        })
      },
      // 获取列表数据
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        // console.log(res)
        this.cateList = res.message
        this.secondList = res.message[0].children
      },
      // 滚动条被点击
      activeChanged(i) {
        this.active = i
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
        this.secondList = this.cateList[i].children
      }
    },
    onLoad() {
      const sysInfo = uni.getSystemInfoSync()
      // console.log(sysInfo) 减去50 表示去除顶部的搜索框
      this.windowHeight = sysInfo.windowHeight - 50
      this.getCateList()
    }
  }
</script>

<style lang="scss">
  // 滚动条区域
  .scroll-view-container {
    display: flex;

    // 左侧一级分类
    .left-scroll-view {
      width: 120px;

      .left-scroll-view-item {
        background-color: #f7f7f7;
        text-align: center;
        font-size: 12px;
        line-height: 60px;

        // 表示 如果同时包含 left-scroll-view-item 和 active 这两个类名,就使用下面的样式
        &.active {
          background-color: #fff;
          position: relative;

          // 渲染激活项左侧的红色指示边线
          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
          }
        }
      }
    }

    // 右侧二级和三级分类
    .right-scroll-view {
      background-color: #fff;

      // 顶部标题
      .right-scroll-top-title {
        font-size: 12px;
        font-weight: bold;
        text-align: center;
        padding: 15px 0;
      }

      // 底部图片
      .right-scroll-bottom-box {
        display: flex;
        flex-wrap: wrap;

        // 每一项的容器
        .right-scroll-item {
          width: 33.33%;
          margin-bottom: 10px;
          display: flex;
          flex-direction: column;
          align-items: center;

          // 文字和图片
          image {
            width: 60px;
            height: 60px;
            // background-color: antiquewhite;
          }

          text {
            font-size: 12px;
          }
        }
      }

    }
  }
</style>
